package net.oschina.app;

import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
import android.widget.EditText;

import com.robotium.solo.Solo;

/**
 * Created by kola on 2017/3/18.
 * 存放公共数据
 * ps：这个case还包含异常处理,切记一定要抛出错误！！！！
 */

public class BaseCase extends ActivityInstrumentationTestCase2<AppStart> {
    private Solo solo;
    private SoloWrap soloWrap;
    public Helper helper;
//    以免报空指针
    boolean isNeedLogin = false;

    public BaseCase() {

        super(AppStart.class);
    }

    public BaseCase(boolean isNeedLogin) {
        super(AppStart.class);
//        当前对象的属性
        this.isNeedLogin = isNeedLogin;

    }

    //   假设全部操作路口都始于首页，先跳到我的，再判断是否需要登陆
    @Override
    public void setUp() throws Exception {
        try {
            super.setUp();
            solo = new Solo(getInstrumentation(), getActivity());
            soloWrap = new SoloWrap(solo);
            helper = new Helper(soloWrap);
//        Log.i("AUTO", "==================1");
//            int b = 3 / 0;
//        solo.waitForText("动弹", 1, 5000, false);
//        solo.clickOnView(solo.getView("net.oschina.app:id/nav_item_me"));
            helper.getPageCommon().goToMeFL();
//        假如需要登陆，我们先判断是否有登陆，如果没有登陆，我们需要点击登陆
            if (isNeedLogin) {
                if (!solo.waitForText("kola73")) {
//               solo.clickOnView(solo.getView("net.oschina.app:id/iv_portrait"));
                    helper.getPageMyUnLogin().clickOnIcon();
//                solo.waitForText("登陆", 1, 5000, false);
//                EditText name = (EditText) solo.getView("net.oschina.app:id/et_username");
//                solo.clearEditText(name);
                    helper.getPageLogin().clearName();
//                solo.enterText(name, "282156710@qq.com");
                    helper.getPageLogin().enterName("282156710@qq.com");
//                EditText pwd = (EditText) solo.getView("net.oschina.app:id/et_password");
//                solo.clearEditText(pwd);
                    helper.getPageLogin().clearPwd();
//                solo.enterText(pwd, "skythebest");
                    helper.getPageLogin().enterPwd("skythebest");
//                solo.clickOnView(solo.getView("net.oschina.app:id/btn_login"));
                    helper.getPageLogin().clickLoginBtn();
                }
                //                假如不需要登陆，我们需要判断是否有登陆，有的话退出，并返回主页
            } else {
                if (solo.waitForText("kola73")) {
                    helper.getPageMyLogin().clickOnSettings();
                    solo.clickOnText("注销");
                }
            }
//        solo.clickOnView(solo.getView("net.oschina.app:id/nav_item_me"));
            helper.getPageCommon().goToNewsFL();

        } catch (Exception ex) {
            Log.i("AUTO", "exception happened in setup section,error msg is" + ex.getMessage());
//            保存图片名以当前类名命名才需要这么写
            solo.takeScreenshot(this.getClass().getSimpleName());
            throw ex;
        }

    }

//    @Override
//    protected void runTest() throws Throwable {
//
//        try {
//            super.runTest();
//        } catch (Throwable th) {
//            if (th != null) {
//                Log.i("AUTO", "exception happened in runtest section,error msg is" + th.getMessage()");
//            }
//            solo.takeScreenshot(this.getClass().getSimpleName());
//            throw th;
//        }
//
//    }


    @Override
    public void tearDown() throws Exception {
        try {
//            String abc = null;
//            abc.length();

        } catch (Throwable th) {
            if (th != null) {
                Log.i("AUTO", "exception happened in teardown section,error msg is" + th.getMessage());
            }
            solo.takeScreenshot(this.getClass().getSimpleName());
            throw th;
        } finally {
            solo.finishOpenedActivities();
//            Log.i("AUTO", "===========================2");
            super.tearDown();
        }

    }
}
